package train48days;

import java.util.ArrayList;
import java.util.Scanner;

class  Node{
    int  x;
    int y;
    public  Node( int x, int y ){
        this.x=x;
        this.y=y;
    }
}
public class 迷宫问题 {
    public static void getMinPath(int[][] mat, int row, int col , int x, int y,
     int[][] book, ArrayList<Node> path,ArrayList<Node> minPath){
        //判断是否yuejie
        if (x<0 || x>=row || y<0 || y>= col || book[x][y]==1
        ||mat[x][y] == 1) return;
        path.add(new Node(x,y));
        //标记当前位置
        book[x][y] = 1;
        if (x == row -1 && y== col -1){
            if (minPath.isEmpty()||  path.size() < minPath.size()){
                minPath.clear();
                for (Node n: path){
                    minPath.add(n);
                }
            }
        }
        getMinPath(mat,row,col, x+1,y,book,path,minPath);
        getMinPath(mat,row,col, x-1,y,book,path,minPath);
        getMinPath(mat,row,col, x,y-1,book,path,minPath);
        getMinPath(mat,row,col, x,y+1,book,path,minPath);
        //吧当前位置从路径删除，寻找新的路径
        path.remove(path.size()-1);
        book[x][y] = 0 ;
    }

    public static void main(String[] args) throws  Exception {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int row = sc.nextInt();
            int col = sc.nextInt();
            int[][] mat = new int[row][col];
            for (int i = 0; i < row; ++i) {
                for (int j = 0; j < col; ++j) {
                    mat[i][j] = sc.nextInt();
                }
            }
            ArrayList<Node> path = new ArrayList<>();
            ArrayList<Node> minPath =new ArrayList<>();
            int[][] book = new int[row][col];
            getMinPath(mat,row,col, 0,0,book,path,minPath);

            for (Node n : minPath){
                System.out.println("(" + n.x+ ","+n.y + ")");
            }
        }
    }
}
